#!/bin/sh /etc/rc.common

START=99
USE_PROCD=1

APPBINARY=/usr/sbin/netdata
CONFIGFILE=/etc/netdata/netdata.conf

config_load 'netdata'

get_config() {
	config_get_bool enabled $1 enabled 0
	config_get port $1 port 19999
	config_get_bool ssl $1 enable_ssl 0
	config_get cert_file $1 cert_file '/etc/ssl/ezopwrt.crt'
	config_get key_file $1 key_file '/etc/ssl/ezopwrt.key'
	config_get_bool ennginx $1 nginx_support 0
	config_get_bool auth $1 auth 0
	config_get userpw $1 user_passwd 'admin:$apr1$t7qQjoqb$YBHtAb7VGSkjIdObMG.Oy0'
	[ "$ennginx" == "1" ] && ssl=0
}

#read_option <option>
read_option() {
	[ -n "$1" ] || return 1
	sed -En "/^\[web\]/,/^(\[|$)/{s|^\s*$1 = *(.*)$|\1|p}" "$CONFIGFILE"
}

#white_option <option> [value]
white_option() {
	[ -n "$1" ] || return 1
	if [ -n "$(sed -En "/^\[web\]/,/^(\[|$)/{s|^\s*($1) =.*$|\1|p}" "$CONFIGFILE")" ]; then
		sed -Ei "/^\[web\]/,/^(\[|$)/{s|^(\s*)$1 =.*|\1$1 = $2|}" "$CONFIGFILE"
	else
		sed -Ei "/^\[web\]/a\\\t$1 = $2" "$CONFIGFILE"
	fi
}

white_conf() {
	if [ "x$ssl" == "x1" ] ;then
	         rm -f $certskey $certscrt
		 hostname=$(uci -q get system.@system[0].hostname | awk '{print tolower($0)}')
		 uci -q set network.lan.hostname=${hostname}
		 uci -q set dhcp.@dnsmasq[0].domain="${hostname}.lan"
	         openssl req -new -newkey rsa:2048 -days 3650 -nodes -x509 -keyout $key_file -out $cert_file -subj "/C=CN/CN=$hostname.lan"
		 uci commit dhcp
		 uci commit network
	else
	   local cert_file='' key_file=''
	fi
	[ "$port" == "$(read_option 'default port')" ] || white_option 'default port' "$port"
	[ "$cert_file" == "$(read_option 'ssl certificate')" ] || white_option 'ssl certificate' "$cert_file"
	[ "$key_file" == "$(read_option 'ssl key')" ] || white_option 'ssl key' "$key_file"
}

write_nginx() {
local location="/etc/nginx/conf.d/netdata.locations"
local htpasswd="/etc/nginx/conf.d/netdata.htpasswd"

echo "$userpw" > "$htpasswd"
[ "$(sed -En "s|^\s*proxy_pass\s+https?://[^:]+:(\d+).*|\1|p" "$location")" == "$port" ] || sed -Ei "/proxy_pass/{s|(\bproxy_pass\b)(\s+.+)?|\1 http://localhost:$port/;|}" "$location"
if [ "$auth" == "1" ];then
	sed -Ei "s|#(\bauth_basic_user_file\b)|\1|" "$location"
else
	sed -Ei "s|^(\s*)(\bauth_basic_user_file\b)|\1#\2|" "$location"
fi
/etc/init.d/nginx reload
}

netdata_instance() {
	mkdir -m 0755 -p /var/cache/netdata
	chown nobody /var/cache/netdata
	mkdir -m 0755 -p /var/lib/netdata
	chown nobody /var/lib/netdata
	mkdir -m 0755 -p /var/lib/netdata/cloud.d
	chown nobody /var/lib/netdata/cloud.d
	mkdir -m 0755 -p /var/log/netdata
	chown nobody /var/log/netdata
	procd_open_instance
	procd_set_param command $APPBINARY -D -c $CONFIGFILE
	procd_set_param file $CONFIGFILE
	procd_set_param respawn
	procd_close_instance
}

start_service() {
	config_foreach get_config 'netdata'
	[ "${enabled:=0}" == "0" ] && stop_service && return 1
	white_conf
	[ "$ennginx" == "1" ] && write_nginx
	netdata_instance
}

stop_service() {
	pgrep -f $APPBINARY | xargs kill -9 >/dev/null 2>&1 
	return 0
}

service_triggers() {
	procd_add_reload_trigger 'netdata'
}
